<?php
////////////////////////////////////////////////////////////////////////////////////////
// Class: ImageGallery
// Purpose: Manage the image gallery
////////////////////////////////////////////////////////////////////////////////////////
require_once 'Image.class.php';

class ImageGallery extends Image {

    private $images;
    private $title;

    function ImageGallery($title) {
        $this->title = $title;
        $this->images = array();
    }

    function getImages(){
        return $this->images;
    }

    function loadGalleryFromXML($title) {
        $dir = "../cmsadmin/view/images/$title";

        //check if Gallery exists
        if (file_exists($dir)) {
            //set new title
            $this->title = $title;
            //remove any images still in the old gallery
            $this->clearGallery();

            //read xml file
            $xml = simplexml_load_file($dir.'/gallery_info.xml');

            $name = null;
            $description = null;;
            $extension = null;;

            foreach($xml->children() as $image) {
                foreach($image->children() as $child) {
                    if($child->getName() == 'name' ) {
                        $name = $child->getName();
                    }
                    if($child->getName() == 'description' ) {
                        $description= $child->getName();
                    }
                    if($child->getName() == 'extension' ) {
                        $extension = $child->getName();
                    }
                }
                $this->images[$name] = new Image($name, $description, $extension);
            }
            return true;
        }else {
            return false;
        }
    }

    function saveGalleryToXML($title){
        $dir = "../cmsadmin/view/images/$title";

        //check if Gallery already exists
        if (file_exists($dir.'/gallery_info.txt')) {
            //delete old
            unlink($dir.'/gallery_info.txt');
        }
        $dom = new DOMDocument("1.0", "UTF-8");

        // create root element
        $root = $dom->createElement("gallery");
        $dom->appendChild($root);

        // create attribute node
        $root_attr = $dom->createAttribute("xmlns:xsi");
        $root->appendChild($root_attr);

        // create attribute value node
        $attr_value = $dom->createTextNode("http://www.w3.org/2001/XMLSchema-instance");
        $root_attr->appendChild($attr_value);

        // create attribute node
        $root_attr2 = $dom->createAttribute("xsi:noNamespaceSchemaLocation");
        $root->appendChild($root_attr2);

        // create attribute value node
        $attr_value2 = $dom->createTextNode("../gallery_schema.xsd");
        $root_attr2->appendChild($attr_value2);

        for($i = 0 ; $i < count($this->images) ; $i++) {
            $image = $this->images[$i];

            // create child element
            $item = $dom->createElement("image");
            $root->appendChild($item);

            // create attribute node
            $id = $dom->createAttribute("id");
            $item->appendChild($id);

            // create attribute value node
            $idValue = $dom->createTextNode($i);
            $id->appendChild($idValue);

            // create child element
            $name = $dom->createElement("name");
            $item->appendChild($name);

            // create text node
            $text = $dom->createTextNode($image->getName());
            $name->appendChild($text);

            // create child element
            $description = $dom->createElement("description");
            $item->appendChild($description);

            // create text node
            $text = $dom->createTextNode($image->getDescription());
            $description->appendChild($text);

            // create child element
            $extension = $dom->createElement("extension");
            $item->appendChild($extension);

            // create text node
            $text = $dom->createTextNode($image->getExtension());
            $extension->appendChild($text);
        }

        // save tree to file
        $dom->save($dir."/gallery_info.xml");

    }

    function saveGalleryToXML() {
        $dir = "../cmsadmin/view/images/$this->title";

        //check if Gallery already exists
        if (file_exists($dir.'/gallery_info.txt')) {
            //delete old
            unlink($dir.'/gallery_info.txt');
        }
        $dom = new DOMDocument("1.0", "UTF-8");

        // create root element
        $root = $dom->createElement("gallery");
        $dom->appendChild($root);

        // create attribute node
        $root_attr = $dom->createAttribute("xmlns:xsi");
        $root->appendChild($root_attr);

        // create attribute value node
        $attr_value = $dom->createTextNode("http://www.w3.org/2001/XMLSchema-instance");
        $root_attr->appendChild($attr_value);

        // create attribute node
        $root_attr2 = $dom->createAttribute("xsi:noNamespaceSchemaLocation");
        $root->appendChild($root_attr2);

        // create attribute value node
        $attr_value2 = $dom->createTextNode("../gallery_schema.xsd");
        $root_attr2->appendChild($attr_value2);

        for($i = 0 ; $i < count($this->images) ; $i++) {
            $image = $this->images[$i];

            // create child element
            $item = $dom->createElement("image");
            $root->appendChild($item);

            // create attribute node
            $id = $dom->createAttribute("id");
            $item->appendChild($id);

            // create attribute value node
            $idValue = $dom->createTextNode($i);
            $id->appendChild($idValue);

            // create child element
            $name = $dom->createElement("name");
            $item->appendChild($name);

            // create text node
            $text = $dom->createTextNode($image->getName());
            $name->appendChild($text);

            // create child element
            $description = $dom->createElement("description");
            $item->appendChild($description);

            // create text node
            $text = $dom->createTextNode($image->getDescription());
            $description->appendChild($text);

            // create child element
            $extension = $dom->createElement("extension");
            $item->appendChild($extension);

            // create text node
            $text = $dom->createTextNode($image->getExtension());
            $extension->appendChild($text);
        }

        // save tree to file
        $dom->save($dir."/gallery_info.xml");

    }

    function addImage(Image $image) {

        $this->images[$image->getName()] = $image;

    }

    function removeImage(Image $image) {

        unset($this->images[$image->getName()]);

    }

    function clearGallery() {

        unset($this->images);
        $this->images = array();

    }
    
    function setTitle($title){
        $this->title = $title;
    }

    function getTitle(){
        return $this->title;
    }
}

?>
